<template>
  <scroll-view scroll-y="true" :style="'height:' +clientHeight+'px;'">
    <view class="home_list">
      <view class="list_md">
        <view class="home_item" v-for="(item,index) in storeList" :key="index">
          <view @tap="clickStore(item.storeId,item.isAdmin)" class="home_name">
            <text class="name">{{item.storeName}}</text>
           <text class="store_id">{{item.storeId}}</text>
          </view>
          <image src="https://mkservice.mk-iot.com/subFile/mk2240/more.png" class="item_icon"
            @tap="clickMore(item.storeId,item.storeName,item.isAdmin)" />
        </view>
      </view>
      <view class="create_home" @tap="gotoCreateStore">
        <uni-icons type="plusempty" size="30"></uni-icons>
        <text>创建门店</text>
      </view>
    </view>
  </scroll-view>
</template>

<script>
  import {myStoreAndShareList,getStore,addStore,updateStore,delStore} from '@/api/system/store/store.js'
  export default {
    data() {
      return {
        clientHeight: 0,
        userId:uni.getStorageSync("userId"),
        storeList:[],
        queryParams: {
          userId: uni.getStorageSync("userId")
        }
      }
    },
    onShow() {
      var that = this;
      uni.getSystemInfo({
        success: function(res) {
          let screenHeight = uni.getSystemInfoSync().windowHeight //获取屏幕高度
          that.clientHeight = screenHeight - 13
        }
      })
      //查询门店列表
      this.getStoreList()
    },
    methods: {
      //查询门店列表
      getStoreList(){
        this.$modal.loading("数据加载中...");
        myStoreAndShareList(this.queryParams).then(response => {
           if(response.code==200){
             // 关闭遮罩层
             this.$modal.closeLoading();
             this.storeList=response.rows;
             uni.stopPullDownRefresh()
           }
        });
      },
      //点击门店更多进行门店删除操作
      clickMore(storeId,storeName,isAdmin) {
        var thant = this;
        if(isAdmin==1){
        uni.showActionSheet({
          alertText: "请选择",
          itemList: ['删除'],
          success: (res) => {
            switch (res.tapIndex) {
              case 0:
                uni.showModal({
                  title: '确定删除'+storeName+'吗？',
                  content: '门店删除后，所有成员会被删除、门店下的分类被清空、已添加设备被解绑和重置',
                  success: function(res) {
                    if (res.confirm) {
                      try {
                        delStore(storeId).then(response => {
                          if (response.code == 200) {
                            uni.showToast({
                              title: '删除成功',
                              duration: 1500,
                              success: function() {
                                thant.getStoreList()
                              }
                            })
                          }
                        })
                      } catch (err) {
                        thant.$modal.msgError(err.msg);
                      }
                    }
                  }
              })
            }
          },
          fail(error) {
            thant.$modal.msgError(error);
          }
        })
        }else{
           this.$modal.msgError("暂无权限");
        }
      },
      //跳转门店详细
      clickStore(id,isAdmin){
         this.$tab.navigateTo('/devicelistpkg/storeManage/storeDetailed?storeId='+id+'&isAdmin='+isAdmin)
      },
      //跳转创建门店
      gotoCreateStore(){
         this.$tab.navigateTo('/devicelistpkg/storeManage/createStore')
      },
    },
    //下拉刷新
    onPullDownRefresh() {
      this.getStoreList()
    },
  }
</script>

<style>
  page {
    background-color: #f9f9f9;
  }

  .home_list {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }

  .home_item {
    background-color: #FFFFFF;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 692rpx;
    height: 98rpx;
    border-radius: 16rpx;
    margin-bottom: 26rpx;
  }

  .home_item:first-child {
    margin-top: 26rpx;
  }

  .home_item>.home_name {
    flex: 1;
    margin-left: 35rpx;
    font-size: 32rpx;
    color: #323232;
    font-weight: 500;
    font-family: PingFang SC;
    display: flex;
    flex-direction: column;
  }

  .home_item>.item_icon {
    width: 20px;
    height: 20px;
    margin-right: 30rpx;
  }

  .create_home {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 692rpx;
    height: 98rpx;
    background-color: #f9f9f9;
    box-shadow: 1px 1px 1px 1px rgba(211, 211, 211, 0.2);
    border: 1px solid rgb(218, 214, 214);
    border-radius: 16rpx;
  }

  .create_home>text {
    color: #1377F9;
    font-size: 32rpx;
    margin-left: 10rpx;
    font-weight: 500;
    font-family: PingFang SC;
  }
  .store_id{
    font-size: 28rpx;color: gray;font-weight: 100;margin-top: 10rpx;
  }
</style>